home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / hsrc_117.zip / EDITOR.C < prev    next >
Text File  |  1990-11-08  |  27KB  |  1,285 lines

  1. /* Editor.C File */
  2.  
  3. #include "msgg.h"
  4. #include "twindow.h"
  5. #include "headedit.h"
  6. #include "keys.h"
  7.  
  8. extern void pascal dcls(void);
  9. extern void pascal dputc(int x, int y, int c);
  10. extern int  pascal dputs(int x, int y, char *s);
  11. extern void pascal dclrwnd(int x1, int y1, int x2, int y2);
  12. extern void pascal dscrollup(int x1, int y1, int x2, int y2);
  13. extern void pascal dscrolldn(int x1, int y1, int x2, int y2);
  14.  
  15. extern word vbase;
  16. extern word maxx;
  17. extern word maxy;
  18. extern char current_color;
  19. extern word videomethod;
  20.  
  21. #define TRUE         1
  22. #define FALSE        0
  23. #define TABS        4
  24. #define NEXTTAB        (TABS-(x%TABS))
  25. #define LASTTAB        (((wwd-1)/TABS)*TABS)
  26. #define PREVTAB        (((x-1)%TABS)+1)
  27. #define curr(x,y)     (bfptr+(y)*wwd+(x))
  28. #define lineno(y)   ((int)(bfptr-topptr)/wwd+(y))
  29. #define sayline(y)  ((unsigned int)((unsigned int)bfptr-(unsigned int)topptr)/(unsigned int)wwd+((unsigned int)y))
  30.  
  31. extern int VSG;
  32. int last_x,last_y;
  33. static char qstr[7];
  34. char keybuf[256]="";
  35. static int quoting=0;
  36. static int wht;
  37. static int wwd;
  38. static int wsz;
  39. static char *topptr;
  40. static char *bfptr;
  41. static char *lstptr;
  42. static int lines;
  43. static char *endptr;
  44. static int blkbeg;
  45. static int blkend;
  46. static int inserting;
  47. static WINDOW *wnd;
  48. static int do_display_text=1;
  49. char abortedit;
  50.  
  51. /* Local function prototypes */
  52.  
  53. extern int  pascal do_macro (int c);
  54. extern char * pascal get_string (char *text,char len,char *deflt,char type);
  55. extern char pascal ask_question (char *text);
  56. void pascal file_funcs(int y,int command);
  57. void pascal save_text(int justblk,int append);
  58. void pascal load_text(int y,int insertit);
  59. void pascal unquote(int *x,int *y);
  60. void pascal quote_mark (int x,int y);
  61. void pascal erase_buffer(int *x,int *y);
  62. char pascal ask_quit(void);
  63. char pascal ask_save(void);
  64. int  pascal lastword(int x,int y);
  65. void pascal last_char(int *x,int *y);
  66. void pascal test_para(int x,int y);
  67. int  pascal trailing_spaces(int y);
  68. int  pascal first_wordlen(int y);
  69. void pascal paraform(int x,int y,char);
  70. int  pascal blankline(int line);
  71. void pascal delete_word(int x,int y);
  72. void pascal delete_line(int y);
  73. void pascal delete_block(void);
  74. void pascal copy_block(int y);
  75. void pascal move_block(int y);
  76. void pascal mvblock(int y,int moving);
  77. void pascal findlast(void);
  78. void pascal find_end(int *x,int *y);
  79. void pascal carrtn(int *x,int *y,int insert);
  80. void pascal backspace(int *x,int *y);
  81. void pascal fore_word(int *x,int *y,char *bf);
  82. int  pascal spaceup(int *x,int *y,char **bf);
  83. void pascal back_word(int *x,int *y,char *bf);
  84. int  pascal spacedn(int *x,int *y,char **bf);
  85. void pascal forward(int *x,int *y);
  86. int  pascal downward(int *y);
  87. void pascal upward(int *y);
  88. void pascal display_text(void);
  89. void pascal disp_line(int y);
  90. void pascal insert_line(void);
  91.  
  92. extern char notefile[133];
  93.  
  94.  
  95.  
  96. /* Process text entry for a window */
  97.  
  98. char * pascal text_editor (WINDOW *wnd1,char *bf,unsigned int bsize) {
  99.  
  100.   char lastun;
  101.   char *b,*buff,*pp;
  102.   int depart=FALSE,i,c;
  103.   int x,y,svx,svlw,tx,tabctr=0;
  104.  
  105.   memset(qstr,0,6);    /* Build quote string */
  106.   *qstr=' ';
  107.   if(*msg.to) {
  108.         qstr[1]=*msg.to;
  109.         pp=strchr(msg.to,' ');
  110.         if(pp && pp[1]) qstr[2]=pp[1];
  111.   }
  112.   strcat(qstr,"> ");
  113.   abortedit=0;
  114.   wnd=wnd1;
  115.   wht=HEIGHT-2;
  116.   wwd=WIDTH-2;
  117.   wsz=wwd*wht;
  118.   topptr=bfptr=bf;
  119.   lines=bsize/wwd;
  120.   endptr=bf+wwd*lines;
  121.   blkbeg=0;
  122.   blkend=0;
  123.   inserting=TRUE;
  124.   x=(maxx-1)-strlen(notefile);
  125.   if(x<49)x=49;
  126.   current_color=readtextcolor + (readtextback * 16);
  127.   lowvideo();
  128.   dprintf(x,6,"%0.28s",notefile);
  129.   x=y=0;
  130.   memset(bf,' ',bsize);
  131.   load_text(0,0);    /* Initial load */
  132.   display_text();
  133.  
  134.   while (TRUE) {
  135.    last_x=COL+1+x;
  136.    last_y=ROW+1+y;
  137.    if(last_y>(maxy-1)) {
  138.     last_y--;
  139.     y--;
  140.    }
  141.    cursor(last_x,last_y);
  142.    buff=curr(x,y);
  143.    if (tabctr) {
  144.       --tabctr;
  145.       c=' ';
  146.    }
  147.    else {
  148.       if(*keybuf) {
  149.         c=*keybuf;
  150.         memmove(keybuf,&keybuf[1],strlen(&keybuf[1])+1);
  151.       }
  152.       else if (quoting) quote_mark(x,y);
  153.       else {
  154.  
  155.         current_color=readtextcolor + (readtextback * 16);
  156.         lowvideo();
  157.         dprintf(3,6,"%3u | %-3u",x+1,sayline(y)+1);
  158.         c=get_char();
  159.       }
  160.       clear_message();
  161.    }
  162.    switch(c) {
  163.       case CTRL_W:                /* Wrap on/off */
  164.     if(!stricmp(notefile,"MSGTMP")) {
  165.         wrapit=1-wrapit;
  166.         if(wrapit) any_message(" Wrap at save off ");
  167.         else any_message(" Wrap at save on ");
  168.         pause();
  169.     }
  170.     break;
  171.       case CTRL_O:                /* Origin toggle */
  172.     if(!stricmp(notefile,"MSGTMP")) {
  173.         usedefault=1-usedefault;
  174.         if(usedefault) {
  175.             get_origin();
  176.         }
  177.         else {
  178.             any_message(" Looking up Origin line in ORIGINS.BBS ");
  179.             pause();
  180.         }
  181.     }
  182.     break;
  183.       case ALT_F1:        /* Spawn keys */
  184.       case ALT_F2:
  185.       case ALT_F3:
  186.       case ALT_F4:
  187.       case ALT_F5:
  188.       case ALT_F6:
  189.       case ALT_F7:
  190.       case ALT_F8:
  191.       case ALT_F9:
  192.       case ALT_F10:
  193.     do_spawn(convertstring(fkey[c-ALT_F1]));
  194.     break;
  195.       case ALT_M:                /* Macros */
  196.     set_help("macrohelp ",0,0);
  197.     any_message(" Press macro key: ");
  198.     c=get_char();
  199.     set_help("edithelp  ",0,0);
  200.     clear_message();
  201.     if(!do_macro(c)) {
  202.         if(c!=ESC) {
  203.             error_message(" Undefined macro key ");
  204.             pause();
  205.         }
  206.     }
  207.     break;
  208.       case ALT_P:                /* Literal */
  209.     any_message(" Press literal key: ");
  210.     set_help("lithelp   ",0,0);
  211.     c=get_char();
  212.     set_help("edithelp  ",0,0);
  213.     clear_message();
  214.     goto LiteralBreakIn;
  215.       case ALT_R:                /* Read block */
  216.     file_funcs(y,4);
  217.     break;
  218.       case ALT_W:                /* Write block */
  219.     file_funcs(y,5);
  220.     break;
  221.       case ALT_L:                /* Load new file */
  222.     file_funcs(y,2);
  223.     break;
  224.       case ALT_S:                /* Save file */
  225.     file_funcs(y,1);
  226.     break;
  227.       case ALT_C:                /* Change filename */
  228.     file_funcs(y,3);
  229.     break;
  230.       case ALT_F:                /* File functions */
  231.     file_funcs(y,0);
  232.     break;
  233.       case CTRL_E:                /* Edit header */
  234.     if(!stricmp(notefile,"MSGTMP")) {
  235.         edit_mess(2);
  236.         show_header(0);
  237.     }
  238.     break;
  239.       case CTRL_U:                /* Unquote text */
  240.     if(blankline(lineno(y)+1)) if (!blankline(lineno(y+1)+1)) downward(&y);
  241.     unquote(&x,&y);
  242.     break;
  243.       case ALT_Q:                /* Set quote string */
  244.     memset(qstr,0,6);    /* Build quote string */
  245.     *qstr=' ';
  246.     if(*msg.to) {
  247.         qstr[1]=*msg.to;
  248.         pp=strchr(msg.to,' ');
  249.         if(pp && pp[1]) qstr[2]=pp[1];
  250.     }
  251.     strcat(qstr,"> ");
  252.     set_help("quotshelp ",0,0);
  253.     pp=get_string("Enter quote string:",6,qstr,'a');
  254.     if(*pp) {
  255.         strcpy(qstr,pp);
  256.         if(strlen(qstr)<6 && qstr[strlen(qstr)-1]!=' ') strcat(qstr," ");
  257.     }
  258.     set_help("edithelp  ",0,0);
  259.     break;
  260.       case CTRL_Q:                /* Quote text */
  261.     if(blankline(lineno(y)+1)) if (!blankline(lineno(y+1)+1)) downward(&y);
  262.     quote_mark(x,y);
  263.     break;
  264.       case '\r':                /* CR */
  265.     carrtn(&x,&y,inserting);
  266.     break;
  267.       case DN:                    /* Down */
  268.         downward(&y);
  269.     break;
  270.       case PGUP:                /* Page Up */
  271.     y=0;
  272.     for(i=0;i<wht;i++) upward(&y);
  273.     x=0;
  274.     break;
  275.       case PGDN:                /* Page Down */
  276.     y=HEIGHT-2;
  277.     for(i=0;i<wht;i++)
  278.       downward(&y);
  279.     y=0;
  280.     break;
  281.       case '\t':                /* Tab */
  282.     if (x + NEXTTAB < wwd) {
  283.       if (inserting)
  284.         tabctr=NEXTTAB;
  285.       else
  286.         x+=NEXTTAB;
  287.     }
  288.     else
  289.       carrtn(&x,&y,inserting);
  290.     break;
  291.       case SHIFT_HT:            /* Shift-tab */
  292.     if (x<TABS) {
  293.       upward(&y);
  294.       x=LASTTAB;
  295.     }
  296.     else
  297.       x-=PREVTAB;
  298.     break;
  299.       case CTRL_FWD:            /* CTRL-=> */
  300.     fore_word(&x,&y,buff);
  301.     break;
  302.       case CTRL_BS:                /* CTRL-<= */
  303.     back_word(&x,&y,buff);
  304.     break;
  305.       case CTRL_PGUP:            /* Top of paragraph */
  306.     while(curr(0,y)>topptr) {
  307.         upward(&y);
  308.         if(blankline(lineno(y)+1)) break;
  309.     }
  310.     break;
  311.       case CTRL_PGDN:
  312.     while(curr(0,y)<endptr) {
  313.         downward(&y);
  314.         if(blankline(lineno(y)+1)) break;
  315.     }
  316.     break;
  317.       case CTRL_B:                /* Bottom of page */
  318.     y=wht-1;
  319.     last_char (&x,&y);
  320.     break;
  321.       case CTRL_T:                /* Top of page */
  322.     x=y=0;
  323.     break;
  324.       case CTRL_HOME:            /* Top of file */
  325.     x=y=0;
  326.     bfptr=topptr;
  327.     display_text();
  328.     break;
  329.       case HOME:                /* Start of line */
  330.     x=0;
  331.     break;
  332.       case CTRL_END:            /* End of file */
  333.     find_end(&x,&y);
  334.     last_char (&x,&y);
  335.     display_text();
  336.     break;
  337.       case END:                    /* End of line */
  338.     last_char (&x,&y);
  339.     break;
  340.       case UP:                    /* up-arrow */
  341.     upward(&y);
  342.     break;
  343.       case CTRL_A:                /* Abort; no sav